﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;

namespace UI
{
    public class NPCUIShopItem : MonoBehaviour
    {
        public UILabel num;
        public UILabel price;
        public UILabel leftTimes;
        public UILabel itemInfo;
        public UILabel recoverLabel;
        public UILabel itemName;
        InstUFShopItem item;
        NPCUIShopPage page;
        int maxCount;
        Countdown refreshCountDown;
        public void SetItem(InstUFShopItem item, NPCUIShopPage page)
        {
            this.item = item;
            this.page = page;
            maxCount = item.totalBuyCount;
            leftTimes.text = item.leftBuyCount + "/" + item.totalBuyCount;
            price.text = item.price + "";
            num.text = item.num + "";

            refreshCountDown = new Countdown(item.refreshIntervalTime / 1000,item.lastRefreshTime, item.totalBuyCount,item.leftBuyCount);
            if (refreshCountDown.GetNextRecoveryTime() > 0)
                isRecovering = true;
            var dictData = Dict.Blo.DictUnionFightItemBlo.GetOne(item.itemId);
            itemInfo.text = dictData.itemInfo;
            itemName.text = dictData.ItemName;
        }

        string GetTimeStringByLeftSeconds(int seconds)
        {
            int s = seconds % 60;
            int m = (seconds / 60) % 60;
            int h = (seconds / 60) / 60;
            return h.ToString("d2") + ":" + m.ToString("d2") + ":" + s.ToString("d2");
        }

        bool isRecovering;
        int recoverTime;
        void Update()
        {
            recoverTime = (int)refreshCountDown.GetNextRecoveryTime();
            recoverLabel.text = GetTimeStringByLeftSeconds(recoverTime);
            leftTimes.text = refreshCountDown.Current + "/" + item.totalBuyCount;
        }

        string GetStr(int num)
        {
            if (num < 10) return "0" + num;
            else
                return num + "";
        }
        public void OnBuyClick()
        {
            if (item.leftBuyCount <= 0)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("field_res_colding"));
                return;
            }
            var playerId = StaticData.playerData.playerId;
            var player = UIUnionFightRunMap.GetNetData().netActor.players[playerId];
            if ((int)player.ownToken < item.price)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("field_res_not_enough"));
                return;
            }
            UIUnionFightRunMap.GetNetData().SendCSUFPlayerBuyItem(item.shopItemId, page.dictData.UFNId + "");
        }
    }
}

